package com.robusta.cityuate.services;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.robusta.cityuate.objects.User;
import com.robusta.cityuate.utils.ConnectionUtils;

public class GetUserService extends HTTPService {

	private final static String REQUEST_URL = "/users/";
	private Context mContext = this;


	@Override
	protected void onResponse() {
		Intent getUserUIupdate = new Intent(assignedFilter);
		switch (responseStatusCode) {
		case HttpStatus.SC_OK: {// successful login
			// remove connection from shared pref
			try {
				JSONObject resObj=new JSONObject(responseString);
			ConnectionUtils.setDisconnecting(mContext, true, assignedFilter,
					resObj.toString());
			// update UI if active
			getUserUIupdate.putExtra(RESUlT, true);
			sendOrderedBroadcast(getUserUIupdate, null);
			Log.e(assignedFilter,"result broadcasted with filter "+ assignedFilter); 
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
			break;
		}
		case HttpStatus.SC_NOT_FOUND: {
			JSONObject res;
			try {
				res = new JSONObject(responseString);
				onNoResponse(res.getString(MESSAGE));
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
		}
	}

	@Override
	protected void onNoResponse(String msg) {
		// remove connection from shared pref keeping result and reason
		ConnectionUtils.setDisconnecting(mContext, false, assignedFilter, msg);
		// update UI if active
		Intent getUserUIupdate = new Intent(assignedFilter);
		getUserUIupdate.putExtra(RESUlT, false);
		sendOrderedBroadcast(getUserUIupdate, null);

	}

	@Override
	protected HttpRequestBase initRequest() {

		return new HttpGet(API_URL + REQUEST_URL + extras.getLong(User.USER_ID));
	}

}
